package app.fugema.shop.main.home.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import app.fugema.shop.databinding.FragmentGoodsClassifyBinding;
import app.fugema.shop.main.goods.adapter.GoodsClassifyAdapter;
import app.fugema.shop.main.goods.adapter.GoodsClassifyLeftAdapter;
import app.fugema.shop.main.goods.model.CategorieBean;
import app.fugema.shop.main.goods.model.CategoryLeftBean;
import app.fugema.shop.main.goods.model.CategoryPageBean;
import app.fugema.shop.main.goods.vm.GoodsVM;

import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.DensityUtil;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.MyFlexboxLayoutManager;
import com.base.common.view.adapter.MyLinearLayoutManager;
import com.base.common.view.adapter.ada.BaseRVAdapter;
import com.base.common.view.adapter.connector.BaseItemTypeInterface;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;

import java.util.ArrayList;
import java.util.List;

import app.fugema.shop.R;
import app.fugema.shop.main.setting.view.SearchActivity;

public class HomeClassifyFragment extends BaseFragment<FragmentGoodsClassifyBinding, GoodsVM> implements OnGetDataListener
{
    @Override
    protected FragmentGoodsClassifyBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_goods_classify, container, false);
    }

    private GoodsClassifyLeftAdapter leftAdapter;
    private GoodsClassifyAdapter adapter;

    private int categoryId;//当前的分类id

    private List<CategoryLeftBean> categoryLeftList;

    @Override
    public void initView()
    {
        super.initView();

        //设置状态栏高度
        int statusBarH = DensityUtil.getStatusBarHeight(getActivity());
        ViewGroup.LayoutParams layoutParams = (ViewGroup.LayoutParams) binding.statusBar.getLayoutParams();
        layoutParams.height = layoutParams.height + statusBarH;
        binding.statusBar.setLayoutParams( layoutParams );

        leftAdapter = new GoodsClassifyLeftAdapter(){
            @Override
            public void onItemClick(View view, BaseRVAdapter adapter, int onclickType, int position, CategoryLeftBean bean)
            {
                super.onItemClick(view, adapter, onclickType, position, bean);

                HomeClassifyFragment.this.adapter.clear();
                HomeClassifyFragment.this.adapter.setDataList( bean.getSubCategory() );
                //categoryId = bean.getId();
                //onGetData(1);
            }
        };
        leftAdapter.setRadio(true);
        binding.rvRecyclerViewLeft.setLayoutManager(new MyLinearLayoutManager(getActivity()));
        binding.rvRecyclerViewLeft.setAdapter(leftAdapter);

        adapter = new GoodsClassifyAdapter();
        binding.rvRecyclerViewRight.setLayoutManager(new MyFlexboxLayoutManager(getActivity()));
        binding.rvRecyclerViewRight.setAdapter(adapter);

        binding.tvSearch.setOnClickListener(new OnClickCheckedUtil() {
            @Override
            public void onClicked(View view) {
                SearchActivity.start(getActivity());
            }
        });
    }


    @Override
    public void initData()
    {
        super.initData();

        viewModel.categoryInfo().observe(this, new BaseViewObserver<LiveDataWrapper<CategoryPageBean>>(this) {
            @Override
            protected void onSuccess(LiveDataWrapper<CategoryPageBean> data)
            {
                categoryLeftList = getCategoryLeftList(data.data);
                leftAdapter.setDataList(categoryLeftList);

                adapter.setDataList( categoryLeftList.get(0).getSubCategory() );
                //categoryId = data.data.getCategory().get(0).getId();
                //onGetData(1);
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<CategoryPageBean> data)
            {
                return UIUtils.isEmpty(data.data.getCategory());
            }
        });

    }

    @Override
    public void onGetData(int pn) {
    }


    private List<CategoryLeftBean> getCategoryLeftList(CategoryPageBean categoryPageBean)
    {
        List<CategoryLeftBean> leftBeans = new ArrayList<>();

        List<CategorieBean> tempLeftList = categoryPageBean.getCategory();

        for (int i = 0; i < tempLeftList.size(); i++)
        {
            CategoryLeftBean  leftBean = new CategoryLeftBean();
            leftBean.setName( tempLeftList.get(i).getName() );
            List<CategorieBean> subLeftCates = getGroupContentCateBeans(tempLeftList.get(i).getSubCategories());
            leftBean.setSubCategory( subLeftCates );
            if(i == 0)
            {
                leftBean.setSelected(true);
            }
            leftBeans.add( leftBean );
        }
        return leftBeans;
    }


    private List<CategorieBean>  getGroupContentCateBeans(List<CategorieBean> subCategories)
    {
        List<CategorieBean> subCates = new ArrayList<>();

        for (CategorieBean subCategory : subCategories)
        {
            subCategory.setItemType(BaseItemTypeInterface.TYPE_GROUP);
            subCates.add( subCategory );

            if( subCategory.getSubCategories() != null && !subCategory.getSubCategories().isEmpty() )
            {
                for (CategorieBean tempCate : subCategory.getSubCategories())
                {
                    subCates.add( tempCate );
                }
            }
        }
        return subCates;
    }



}
